# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
import json


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "阳光出行保交通工具意外险"

    # 生产产品数据
    def genProductInfo(self):

        plan = {
            "planA": {
                "object": [
                    {
                        "airplane_death": "200000",
                        "train_death": "200000",
                        "ship_disability": "200000",
                        "passenger_death": "200000"
                    }
                ],
                "days": ["1m", "3m", "1y"]
            },
            "planB": {
                "object": [
                    {
                        "airplane_death": "300000",
                        "train_death": "300000",
                        "ship_disability": "300000",
                        "passenger_death": "300000"
                    },

                ],
                "days": ["1m", "3m", "1y"]
            },
            "planC": {
                "object": [
                    {
                        "airplane_death": "500000",
                        "train_death": "500000",
                        "ship_disability": "500000",
                        "passenger_death": "500000"
                    },

                ],
                "days": ["1m", "3m", "1y"]
            }
        }

        # 价格表
        prices = ["11", "33", "110", "16.5", "49.5", "165", "27.5", "82.5", "275"]
        # 计数变量
        num = 0
        for k in plan:
            for v in product(plan[k]["object"], plan[k]["days"]):
                data = {
                    "name": self.name,
                    "company": "阳光保险集团",
                    "days": v[1],
                    "people_count": "1",
                    "price": Helper.decimalParse(prices[num])

                }
                num += 1

                data.update(v[0])
                self.mergeTable(data)

    # 合并表格
    def mergeTable(self, data):

        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`,`train_death`,`ship_disability`,`passenger_death`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}","{train_death}","{ship_disability}","{passenger_death}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':
    a = Arrangement()
    a.genProductInfo()
